﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CubicFWNet.Application.SystemManage;
using CubicFWNet.Mvc.Filter;
using CubicFWNet.Mvc.Models;
using CubicFWNet.Common;
using CubicFWNet.Data.SystemManage.Model;
using CubicFWNet.Mvc.Areas.SystemManage.Models;
using CubicFWNet.Application;

namespace CubicFWNet.Mvc.Areas.SystemManage.Controllers
{
    public class ItemController : ControllerBase
    {
        private ItemApp _itemApp;
        public ItemController()
        {
            _itemApp = new ItemApp();
        }
        //[Authenticate]
        [HttpGet]
        public ViewResult Index(string id)
        {
            var category = Guid.Empty;
            if (!string.IsNullOrEmpty(id))
            {
                category = Guid.Parse(id);
            }
            ViewBag.CategoryItems = _itemApp.GetItemCategory();
            var viewModel = new ItemListViewModel
            {
                CategoryItemId = id,                
                Items = _itemApp.GetItems(category),
                Item = new Item()
            };
            return View(viewModel);
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(Guid ItemId)
        {
            var data = _itemApp.GetItem(ItemId);
            return Content(data.ToJson());
        }
        [Delete]
        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {
            return _itemApp.Delete(id).ToJson();
        }

        //添加或修改角色
        [HttpPost]
        [HandlerAjaxOnly]
        [Insert]
        public string Save(Item item)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.CategoryItems = _itemApp.GetItemCategory();
                Result.Status = ResultType.error.ToString();
                Result.Data = this.RenderPartialViewToString("_ItemForm", item);
                return Result.ToJson();
            }
            
            item.CreatorId = this.CurrentUserId;            
            _itemApp.AddOrUpdate(item);

            Result.Status = ResultType.success.ToString();
            Result.Data = "操作成功";
            Result.Message = "操作成功";
            return Result.ToJson();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetItemCategoryJson()
        {
            var data = _itemApp.GetItemCategory();
            var treeList = new List<TreeViewModel>();
            treeList.Add(new TreeViewModel()
            {
                id = Guid.Empty.ToString(),
                text = "全部",
                value = "",
                parentId = "",
                isexpand = true,
                complete = true,
                hasChildren = false
            });
            foreach (ItemCategory item in data)
            {
                TreeViewModel tree = new TreeViewModel();
                tree.id = item.Id.ToString();
                tree.text = item.Name;
                tree.value = item.Id.ToString();
                tree.parentId = "";
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }

    }
}